// ==UserScript==
// @name         (萌新慎用)隔时修改资源数脚本
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  reset Resource Data!
// @author       Miya
// @match        https://likexia.gitee.io/evolve/
// @downloadURL  https://gitee.com/miya0716/evolve-cheat-script/raw/master/main.js
// @updateURL    https://gitee.com/miya0716/evolve-cheat-script/raw/master/main.js
// @grant        none
// @run-at       document-body
// ==/UserScript==

//重置数据
function reloadData() {
    let dataString = LZString.decompressFromUTF16(localStorage.getItem('evolved'));
    let data = JSON.parse(dataString);

    localStorage.removeItem('evolveBak');

    //修改数据 修改所有有max屬性的資源 amount=max
    let keys = Object.keys(data.resource);
    for (let key of keys) {
        if (!data.resource[key]) continue;
        if (data.resource[key].max <= 0) {
            data.resource[key].amount = Math.pow(10, 10);
        } else {
            data.resource[key].amount = data.resource[key].max;
        }
    }

    //自定义资源修改数据 可随自己的情况修改
    /**
     * 数据可以通过一下代码获取
     *     let dataString = LZString.decompressFromUTF16(localStorage.getItem('evolved'));
     *     let data = JSON.parse(dataString);
     *     console.log(data);
     */
    let autoResetData = {
        "质粒": {path: "data.race.Plasmid.count", value: 10000000000},
        "反质粒": {path: "data.race.Plasmid.anti", value: 10000000000},
        "噬菌体": {path: "data.race.Phage.count", value: 10000000000},
        "暗能量": {path: "data.race.Dark.count", value: 10000000000},
        "和谐水晶": {path: "data.race.Harmony.count", value: 10000000000},
        "基因": {path: "data.resource.Genes.amount", value: 10000000000},
        "鲜血之石": {path: "data.resource.Blood_Stone.value", value: 10000000000}
    };

    for (let name in autoResetData) {
        eval(autoResetData[name].path + ' = ' + autoResetData[name].value);
    }
    //修改数据 end

    dataString = LZString.compressToUTF16(JSON.stringify(data));
    localStorage.setItem('evolved', dataString);
    //刷新页面
    setTimeout(() => {
        window.location.href = window.location.href;
    }, 100)
}

//生成设置界面 时而会无法显示 建议开始Console控制面板
function buildForm() {
    let reloadDataformStyleStr = "<style>\n" +
        "    .reload-data-form {\n" +
        "        position: absolute;\n" +
        "        z-index: 10000;\n" +
        "        background: #efefef;\n" +
        "        border: 1px solid #ff552e;\n" +
        "        border-radius: 5px;\n" +
        "        padding: 10px;\n" +
        "        font: 400 12px/24px \"微软雅黑\";\n" +
        "        width: 300px;\n" +
        "        color: #666;\n" +
        "        right: 10px;\n" +
        "        bottom: 10px;\n" +
        "    }\n" +
        "\n" +
        "    .reload-data-form label {\n" +
        "        display: block;\n" +
        "        color: #666;\n" +
        "    }\n" +
        "\n" +
        "    .reload-data-form button {\n" +
        "        width: 100%;\n" +
        "        margin-top: 15px;\n" +
        "    }\n" +
        "</style>";

    let reloadDataFormStr = "<form class='reload-data-form'>\n" +
        "    <h4>Miya 挂逼脚本</h4>\n" +
        "    <span class=\"reload-data-time-count-down\"></span>\n" +
        "    <label>\n" +
        "        是否开启脚本:\n" +
        "        <select name=\"open_command\">\n" +
        "            <option value=\"1\">开启</option>\n" +
        "            <option value=\"2\">关闭</option>\n" +
        "        </select>\n" +
        "    </label>\n" +
        "    <label>重置资源时间(毫秒):<input type=\"text\" name=\"reload_data_time\" value=\"15000\"></label>\n" +
        "    <button type=\"button\" class=\"reload-data-set-btn\">保存</button>\n" +
        "</form>";

    $('body').append(reloadDataformStyleStr + reloadDataFormStr);

    //初始化表单
    $(function () {
        let open_command = localStorage.getItem('open_command') ?? 2;
        let reload_data_time = localStorage.getItem('reload_data_time') ?? 15000;

        $('select[name="open_command"]').val(open_command);
        $('input[name="reload_data_time"]').val(reload_data_time);
    })

    $('.reload-data-set-btn').on('click', function () {
        let open_command = $('select[name="open_command"]').val();
        let reload_data_time = $('input[name="reload_data_time"]').val();

        localStorage.setItem('open_command', open_command);
        localStorage.setItem('reload_data_time', reload_data_time);
        console.log('完成设置,下次刷新可用!');
        location.href = location.href;
    });
}

(function () {
    'use strict';

    //是否开启重置数据
    let openCommand = localStorage.getItem('open_command') ?? 2;
    //设置重置数据时间
    let reloadDataTime = localStorage.getItem('reload_data_time') ?? 15000;

    //显示设置界面
    buildForm();

    //开始定时器
    if (openCommand == 1) {
        let second = reloadDataTime / 1000;
        let key = null;

        //开启倒计时
        setInterval(() => {
            second -= 1;
            $('.reload-data-time-count-down').text(second + '秒后重置数据!');
        }, 1000);

        //开启数据刷新
        setTimeout(reloadData, reloadDataTime)
    }
})();